﻿using ClusterAware;
using ClusterAwareAdmin;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ClusterAwareProperties;
using System.Linq;

namespace ClusterAwareTest
{


    /// <summary>
    ///This is a test class for ResourceMethodsTest and is intended
    ///to contain all ResourceMethodsTest Unit Tests
    ///</summary>
    [TestClass]
    public class ResourceMethodsTest
    {
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        private static ClusterManager _manager;

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize]
        public static void ClassTestInitialize(TestContext testContext)
        {
            var managerInit = new ClusterManagerAdmin
            {
                Username = SharedConsts.User,
                Password = SharedConsts.Pwd,
                Domain = SharedConsts.Domain,
                Machine = SharedConsts.Machine,
                CacheInstances = false
            };
            managerInit.Connect();
            if (!managerInit.Groups.ExistGroup(SharedConsts.NewGroupTest))
            {
                managerInit.Groups.Create(SharedConsts.NewGroupTest);
            }
            if (!managerInit.Groups.ExistGroup(SharedConsts.GroupTest))
            {
                managerInit.Groups.Create(SharedConsts.GroupTest);
            }
            if (managerInit.OperationSystem == WindowsOperationSystem.Windows2008 || managerInit.OperationSystem == WindowsOperationSystem.Windows2008R2)
            {
                var node = managerInit.Groups.GetActiveNode(SharedConsts.GroupTest).Name;
                managerInit.Groups.SetPreferredOwners(SharedConsts.GroupTest, new[] { node });
            }
            if (!managerInit.Resources.ExistResource(SharedConsts.ResourceDep))
            {
                managerInit.Resources.Create(SharedConsts.GroupTest, SharedConsts.ResourceDep, ResourceTypeNamesConstants.ResourcetypeGenericApplication, false);
                var res = managerInit.Resources.GetResource(SharedConsts.ResourceDep);
                res.PrivateProperties.SetPropertyValue("CommandLine", "Notepad.exe");
                res.PrivateProperties.SetPropertyValue("CurrentDirectory", "C:\\");
                res.PrivateProperties.SetPropertyValue("InteractWithDesktop", true);
                managerInit.Resources.Save(res);
            }
            if (!managerInit.Resources.ExistResource(SharedConsts.ResourceTest))
            {
                managerInit.Resources.Create(SharedConsts.GroupTest, SharedConsts.ResourceTest, ResourceTypeNamesConstants.ResourcetypeGenericApplication, false);
                var res = managerInit.Resources.GetResource(SharedConsts.ResourceTest);
                res.PrivateProperties.SetPropertyValue("CommandLine", "Notepad.exe");
                res.PrivateProperties.SetPropertyValue("CurrentDirectory", "C:\\");
                res.PrivateProperties.SetPropertyValue("InteractWithDesktop", true);
                managerInit.Resources.Save(res);
                managerInit.Resources.AddDependency(SharedConsts.ResourceTest, SharedConsts.ResourceDep);
            }
            managerInit.Dispose();
        }
        //
        //Use ClassCleanup to run code after all tests in a class have run
        [ClassCleanup]
        public static void ClassTestCleanup()
        {
            var managerInit = new ClusterManagerAdmin
            {
                Username = SharedConsts.User,
                Password = SharedConsts.Pwd,
                Domain = SharedConsts.Domain,
                Machine = SharedConsts.Machine,
                CacheInstances = false
            };
            managerInit.Connect();
            if (managerInit.Resources.ExistResource(SharedConsts.ResourceDep))
            {
                managerInit.Resources.Delete(SharedConsts.ResourceDep);
            }
            if (managerInit.Resources.ExistResource(SharedConsts.ResourceTest))
            {
                managerInit.Resources.Delete(SharedConsts.ResourceTest);
            }
            if (managerInit.Groups.ExistGroup(SharedConsts.NewGroupTest))
            {
                managerInit.Groups.Delete(SharedConsts.NewGroupTest);
            }
            if (managerInit.Groups.ExistGroup(SharedConsts.GroupTest))
            {
                managerInit.Groups.Delete(SharedConsts.GroupTest);
            }
            managerInit.Dispose();
        }
        //
        //Use TestInitialize to run code before running each test
        [TestInitialize]
        public void Initialize()
        {
            _manager = new ClusterManager
            {
                Username = SharedConsts.User,
                Password = SharedConsts.Pwd,
                Domain = SharedConsts.Domain,
                Machine = SharedConsts.Machine,
                CacheInstances = false
            };
            _manager.Connect();
        }
        //
        //Use TestCleanup to run code after each test has run
        [TestCleanup]
        public void Cleanup()
        {
            _manager.Dispose();
        }
        //
        #endregion

        /// <summary>
        ///A test for TakeOffline
        ///</summary>
        [TestMethod]
        public void TakeOfflineTest()
        {
            _manager.CacheInstances = true;
            var target = new ResourceMethods(_manager);
            target.TakeOffline(SharedConsts.ResourceTest, 10);
            Assert.AreEqual(StateResources.Offline, target.GetResource(SharedConsts.ResourceTest).State);
            _manager.CacheInstances = false;
             target = new ResourceMethods(_manager);
            target.TakeOffline(SharedConsts.ResourceTest, 10);
            Assert.AreEqual(StateResources.Offline, target.GetResource(SharedConsts.ResourceTest).State);
        }


        /// <summary>
        ///A test for Save
        ///</summary>
        [TestMethod]
        public void SaveTest()
        {
            ClassTestInitialize(TestContext);
            _manager.CacheInstances = true;
            var target = new ResourceMethods(_manager);
            var resClass = target.GetResource(SharedConsts.ResourceTest);
            var olddes = resClass.Description;
            resClass.Description = "SaveTest";
            const string expected = "SaveTest";
            var actual = target.Save(resClass, true, 10);
            Assert.AreEqual(expected, actual.Description);
            resClass.Description = olddes;
            actual = target.Save(resClass, true, 10);
            Assert.AreEqual(olddes, actual.Description);
            _manager.CacheInstances = false;
             target = new ResourceMethods(_manager);
             resClass = target.GetResource(SharedConsts.ResourceTest);
             olddes = resClass.Description;
            resClass.Description = "SaveTest";
             actual = target.Save(resClass, true, 10);
            Assert.AreEqual(expected, actual.Description);
            resClass.Description = olddes;
            actual = target.Save(resClass, true, 10);
            Assert.AreEqual(olddes, actual.Description);
        }

        /// <summary>
        ///A test for RenewAddress
        ///</summary>
        [TestMethod]
        public void RenewAddressTest()
        {
            if (_manager.OperationSystem == WindowsOperationSystem.Windows2003 || _manager.OperationSystem == WindowsOperationSystem.Windows2003R2)
            {
                Assert.Inconclusive("Windows 2003 not suport Method RenewAddress");
            }
            Assert.Inconclusive("Put resource name with resourcetype IP Adress with DHCP options for test");
            //_manager.CacheInstances = true;
            //var target = new ResourceMethods(_manager);
            //target.RenewAddress(<put Name resource>);
            //_manager.CacheInstances = false;
            // target = new ResourceMethods(_manager);
            //target.RenewAddress(<put Name resource>);
        }

        /// <summary>
        ///A test for ReleaseAddress
        ///</summary>
        [TestMethod]
        public void ReleaseAddressTest()
        {
            if (_manager.OperationSystem == WindowsOperationSystem.Windows2003 || _manager.OperationSystem == WindowsOperationSystem.Windows2003R2)
            {
                Assert.Inconclusive("Windows 2003 not suport Method RenewAddress");
            }
            Assert.Inconclusive("Put resource name with resourcetype IP Adress with DHCP options for test");
            //_manager.CacheInstances = true;
            //var target = new ResourceMethods(_manager);
            //target.ReleaseAddress(<put Name resource>);
            //_manager.CacheInstances = false;
            // target = new ResourceMethods(_manager);
            //target.ReleaseAddress(<put Name resource>);
        }

        /// <summary>
        ///A test for MoveToNewGroup
        ///</summary>
        [TestMethod]
        public void MoveToNewGroupTest()
        {
            ClassTestInitialize(TestContext);
            _manager.CacheInstances = true;
            var target = new ResourceMethods(_manager);
            target.MoveToNewGroup(SharedConsts.ResourceTest, SharedConsts.NewGroupTest);
            var actual = _manager.Groups.GetResources(SharedConsts.NewGroupTest).Select(r => r.Name == SharedConsts.ResourceTest).FirstOrDefault();
            Assert.AreNotEqual(null, actual);
            target.MoveToNewGroup(SharedConsts.ResourceTest, SharedConsts.GroupTest);
            actual = _manager.Groups.GetResources(SharedConsts.GroupTest).Select(r => r.Name == SharedConsts.ResourceTest).FirstOrDefault();
            Assert.AreNotEqual(null, actual);
            _manager.CacheInstances = false;
             target = new ResourceMethods(_manager);
            target.MoveToNewGroup(SharedConsts.ResourceTest, SharedConsts.NewGroupTest);
             actual = _manager.Groups.GetResources(SharedConsts.NewGroupTest).Select(r => r.Name == SharedConsts.ResourceTest).FirstOrDefault();
            Assert.AreNotEqual(null, actual);
            target.MoveToNewGroup(SharedConsts.ResourceTest, SharedConsts.GroupTest);
            actual = _manager.Groups.GetResources(SharedConsts.GroupTest).Select(r => r.Name == SharedConsts.ResourceTest).FirstOrDefault();
            Assert.AreNotEqual(null, actual);
        }

        /// <summary>
        ///A test for GetResources
        ///</summary>
        [TestMethod]
        public void GetResourcesTest()
        {
            _manager.CacheInstances = true;
            var target = new ResourceMethods(_manager);
            var actual = target.GetResources();
            Assert.AreNotEqual(0, actual.Count());
            var resactual = actual.Select(r => r.Name == SharedConsts.ResourceTest).FirstOrDefault();
            Assert.AreNotEqual(null, resactual);
            _manager.CacheInstances = false;
             target = new ResourceMethods(_manager);
             actual = target.GetResources();
            Assert.AreNotEqual(0, actual.Count());
             resactual = actual.Select(r => r.Name == SharedConsts.ResourceTest).FirstOrDefault();
            Assert.AreNotEqual(null, resactual);
        }

        /// <summary>
        ///A test for GetResource
        ///</summary>
        [TestMethod]
        public void GetResourceTest()
        {
            _manager.CacheInstances = true;
            var target = new ResourceMethods(_manager);
            var actual = target.GetResource(SharedConsts.ResourceTest);
            Assert.AreNotEqual(null, actual);
            Assert.AreEqual(SharedConsts.ResourceTest, actual.Name);
            _manager.CacheInstances = false;
             target = new ResourceMethods(_manager);
             actual = target.GetResource(SharedConsts.ResourceTest);
            Assert.AreNotEqual(null, actual);
            Assert.AreEqual(SharedConsts.ResourceTest, actual.Name);
        }

        /// <summary>
        ///A test for GetPossibleOwnerNode
        ///</summary>
        [TestMethod]
        public void GetPossibleOwnerNodeTest()
        {
            _manager.CacheInstances = true;
            var target = new ResourceMethods(_manager);
            var actual = target.GetPossibleOwnerNode(SharedConsts.ResourceTest);
            Assert.AreNotEqual(null, actual);
            _manager.CacheInstances = false;
             target = new ResourceMethods(_manager);
             actual = target.GetPossibleOwnerNode(SharedConsts.ResourceTest);
            Assert.AreNotEqual(null, actual);
        }

        /// <summary>
        ///A test for GetDisks
        ///</summary>
        [TestMethod]
        public void GetDisksTest()
        {
            if (_manager.OperationSystem == WindowsOperationSystem.Windows2003 || _manager.OperationSystem == WindowsOperationSystem.Windows2003R2)
            {
                Assert.Inconclusive("Windows 2003 not suport Method GetDisks");
            }
            _manager.CacheInstances = true;
            var target = new ResourceMethods(_manager);
            var actual = target.GetDisks(SharedConsts.ResourceTest);
            Assert.AreNotEqual(null, actual);
            _manager.CacheInstances = false;
             target = new ResourceMethods(_manager);
             actual = target.GetDisks(SharedConsts.ResourceTest);
            Assert.AreNotEqual(null, actual);
        }

        /// <summary>
        ///A test for GetDependenciesResource
        ///</summary>
        [TestMethod]
        public void GetDependenciesResourceTest()
        {
            _manager.CacheInstances = true;
            var target = new ResourceMethods(_manager);
            var actual = target.GetDependenciesResource(SharedConsts.ResourceTest);
            var resactual = actual.Select(r => r.Name == SharedConsts.ResourceDep).FirstOrDefault();
            Assert.AreNotEqual(null, resactual);
            _manager.CacheInstances = false;
             target = new ResourceMethods(_manager);
             actual = target.GetDependenciesResource(SharedConsts.ResourceTest);
             resactual = actual.Select(r => r.Name == SharedConsts.ResourceDep).FirstOrDefault();
            Assert.AreNotEqual(null, resactual);
        }

        /// <summary>
        ///A test for GetDependencieseExpression
        ///</summary>
        [TestMethod]
        public void GetDependencieseExpressionTest()
        {
            if (_manager.OperationSystem == WindowsOperationSystem.Windows2003 || _manager.OperationSystem == WindowsOperationSystem.Windows2003R2)
            {
                Assert.Inconclusive("Windows 2003 not suport Method GetDependencieseExpression");
            }
            _manager.CacheInstances = true;
            var target = new ResourceMethods(_manager);
            var actual = target.GetDependencieseExpression(SharedConsts.ResourceTest);
            Assert.AreNotEqual(null, actual);
            _manager.CacheInstances = false;
            target = new ResourceMethods(_manager);
            actual = target.GetDependencieseExpression(SharedConsts.ResourceTest);
            Assert.AreNotEqual(null, actual);
        }

        /// <summary>
        ///A test for FailResource
        ///</summary>
        [TestMethod]
        public void FailResourceTest()
        {
            _manager.CacheInstances = true;
            var target = new ResourceMethods(_manager);
            target.FailResource(SharedConsts.ResourceTest);
            _manager.CacheInstances = false;
             target = new ResourceMethods(_manager);
            target.FailResource(SharedConsts.ResourceTest);
        }

        /// <summary>
        ///A test for ExistResource
        ///</summary>
        [TestMethod]
        public void ExistResourceTest()
        {
            _manager.CacheInstances = true;
            var target = new ResourceMethods(_manager);
            var result = target.ExistResource(SharedConsts.ResourceTest);
            Assert.AreEqual(true, result);
            result = target.ExistResource(SharedConsts.ResourceTest + "NotExist");
            Assert.AreEqual(false, result);
            _manager.CacheInstances = false;
             target = new ResourceMethods(_manager);
             result = target.ExistResource(SharedConsts.ResourceTest);
            Assert.AreEqual(true, result);
            result = target.ExistResource(SharedConsts.ResourceTest + "NotExist");
            Assert.AreEqual(false, result);
        }

        /// <summary>
        ///A test for BringOnline
        ///</summary>
        [TestMethod]
        public void BringOnlineTest()
        {
            ClassTestInitialize(TestContext);
            _manager.CacheInstances = true;
            var target = new ResourceMethods(_manager);
            if (target.GetResource(SharedConsts.ResourceTest).State == StateResources.Offline)
            {
                target.BringOnline(SharedConsts.ResourceTest, 10);
            }
            target.BringOnline(SharedConsts.ResourceTest, 10);
            Assert.AreEqual(StateResources.Online, target.GetResource(SharedConsts.ResourceTest).State);
            _manager.CacheInstances = false;
             target = new ResourceMethods(_manager);
             if (target.GetResource(SharedConsts.ResourceTest).State == StateResources.Offline)
            {
                target.BringOnline(SharedConsts.ResourceTest, 10);
            }
            target.BringOnline(SharedConsts.ResourceTest, 10);
            Assert.AreEqual(StateResources.Online, target.GetResource(SharedConsts.ResourceTest).State);
        }

        /// <summary>
        ///A test for ResourceMethods Constructor
        ///</summary>
        [TestMethod]
        public void ResourceMethodsConstructorTest()
        {
            _manager.CacheInstances = true;
            var target = new ResourceMethods(_manager);
            Assert.AreNotEqual(null, target);
            _manager.CacheInstances = false;
             target = new ResourceMethods(_manager);
            Assert.AreNotEqual(null, target);
        }
    }
}
